class Solution {
public:
    string addBinary(string a, string b) {
        int i = a.size() - 1, j = b.size() - 1;
        string ret;
        int next = 0;
        while(i >= 0 || j >= 0)
        {
            char x, y;
            if(i < 0)
                x = '0';
            else
                x = a[i--];
            if(j < 0)
                y = '0';
            else
                y = b[j--];
            
            if(x == '1' && y == '1')
            {
                ret = to_string(next) + ret;
                next = 1;
            }
            else if(x == '0' && y == '0')
            {
                ret = to_string(next) + ret;
                next = 0;
            }
            else if(((x == '1' && y == '0') || (x == '0' && y == '1')))
                ret = (next == 1 ? '0' : '1') + ret;

        }
        if(next == 1)
            ret = '1' + ret;
        return ret;
    }
};